package com.yuxi.al.leercode;

import java.util.ArrayList;
import java.util.List;

/**
 * 给一个正数，找出这个正数的反转后的二进制对应的正数
 * <p>
 * eg: 5 101 reverse 010 对应的为2
 * Created by yuxi on 2017/4/1.
 */
public class FindComplement {
    public static void main(String[] args) {
        int index = 15;
        int res = findComlement(index);
        System.out.println(res);
    }

    private static int findComlement(int index) {
        int res = 0;
        List<Integer> list = new ArrayList();
        while (index != 0) {
            int value = index % 2;
            list.add(value);
            index = index / 2;
        }
        for (int i = list.size() - 1; i >= 0; i--) {
            int j = list.get(i);
            if ((j ^ 1) == 1) {
                res = res * 2 + 1;
            } else {
                res = res * 2 + 0;
            }
        }
        return res;
    }
}
